import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx'
const { TableStore } = WeaTableNew;
import { ListStore } from './listStore';
import { Modal, message } from "antd"
import * as Apis from "../apis/capital"
import * as Common_Apis from "../apis/index"
import { ShareStore } from "./shareStore"
import validatorjs from 'validatorjs';
import { getFormInitDatas } from "../util/index"
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class MineCptStore extends ListStore {
    title = getLabel(1209, "我的资产");
    @observable baseParams = {
    }
    @observable searchParams = {
        name: ""
    }
    @observable showSearchAd = false;
    @observable rightMenu = [];
    condition = [];
    conditiontype = { type: "mycpt" };
    @observable capitalid = "";   //资产id

    @observable showmend = false;
    @observable mendInfo = [];
    @observable mendForm = new WeaForm();
    //日志
    @observable showlog = false;
    @observable logCondition = [];
    @observable logForm = new WeaForm();
    @observable logTableStore = new TableStore();
    //资产编辑
    @observable showEditDialog = false;
    @observable cptInfo = {};
    @observable cptForm = new WeaForm();
    validateRules = {};

    @observable shareStore = new ShareStore();
    //流转 变更
    @observable showFlowModify = false;
    @observable showType = "";
    @observable hasFlowRight = false; //资产流转查看维护权限
    @observable hasVerified = false;
    @observable flowForm = new WeaForm();
    @observable cptCardCondition = [];
    @observable flowTableStore = new TableStore();
    //xiangqing
    @observable flowDetailCondition = [];
    @observable flowDetailForm = new WeaForm();

    @observable modifyForm = new WeaForm();
    @observable cptModifyCondition = [];
    @observable modifyTableStore = new TableStore();
    @observable cptFlowDeltail = false;

    @observable treeSearchParams = {};

    @observable leftShow = false;

	@observable modifylogVisible = false;//资产资料日志是否显示

    @observable position = [];

    @observable address = "";

    @observable
    showLeftTree = (bool) => {
        this.leftShow = bool;
    }
    appDetachDisableAll = false;
    @action
    initDatas = (params = {}) => {
        this.form = new WeaForm();
        let newParams = { ...this.conditiontype, ...params };
        this.title = this.title;
        Apis.getTreeDatas(newParams).then(data => {
            this.leftTree = data.treedata;
            this.leftTreeCount = data.treecount;
            this.leftTreeCountType = data.countcfg;
            this.selectedTreeKey = "";
        });
        Apis.getRightMenu(newParams).then(data => {
            this.rightMenu = data.rightMenus;
        });
        Apis.getCondition(newParams).then(data => {
            this.condition = data.condition;
            //根据高级搜索条件初始化form
            this.form && !this.form.isFormInit && this.form.initFormFields(data.condition);
        })
        Apis.getDetachable().then((data) => {
            this.appDetachDisableAll = data.appDetachDisableAll;
        });

    }
    @action
    updateLeftTree = (params = {}) => {
        let newParams = { ...this.conditiontype, ...params };
        Apis.getTreeDatas(newParams).then(data => {
            this.leftTree = data.treedata;
            this.leftTreeCount = data.treecount;
            this.leftTreeCountType = data.countcfg;
            this.selectedTreeKey = "";
        });
    }

    @action
    initTreeDatas = (params = {}) => {
        Apis.getTreeDatas({ ...this.conditiontype, ...params }).then(data => {
            this.leftTree = data.treedata;
            this.leftTreeCount = data.treecount;
            this.leftTreeCountType = data.countcfg;
            this.selectedTreeKey = this.selectedTreeKey || data.selectedKeys || "";
        })
    }

    doSearch = (params = {}, type = "") => {
        this.loading = true;
        //获取表单的参数值
        const searchParamsAd = this.form.getFormParams();
        const newParams = { ...this.conditiontype, ...this.searchParams, ...searchParamsAd, ...this.treeSearchParams, ...params };
        if (type == "tree") {
            this.treeSearchParams = { ...this.treeSearchParams, ...params };
        }
        Apis.getCptList(newParams).then(data => {
            this.tableStore = new TableStore();
            this.tableStore.getDatas(data.sessionkey, params.current || 1);
            this.searchParams = { ...this.searchParams, ...params };
            this.dataKey = data.sessionkey;
            this.loading = false;
        })
    }


    setSelectedTreeKey = (key) => {
        this.selectedTreeKey = key;
    }
    //导出
    cptExport = (params = {}) => {
        const searchParamsAd = this.form.getFormParams();
        const newParams = { ...this.conditiontype, ...this.searchParams, ...searchParamsAd, ...toJS(this.treeSearchParams), ...params };
        Common_Apis.cptImport(newParams).then(data => {
            window.location = "/weaver/weaver.file.ExcelOut";
        }).catch(err => message.error(err))
    }
    //保存单行资产id
    saveCapitalId = (id) => {
        this.capitalid = id;
    }
    //归还
    onGiveback = (params = {}, callBack) => {
        Apis.doCptManager(params).then(data => {
            if (data.msg == '') {
                this.showEditDialog = false;
                message.success(getLabel(83560, '归还成功！'));
                typeof callBack == "function" && callBack();//刷新查询列表
                this.doSearch();//刷新我的资产列表
            } else {
                Modal.error({
                    title: getLabel(15172, '系统提示'),
                    content: data.msg,
                });
            }
        }).catch(err => message.error(err))
    }
    //送修
    doMend = (bool, params = {}) => {
        this.showmend = bool;
        if (bool) {
            this.mendForm = new WeaForm();
            this.capitalid = params.capitalid;
            Apis.doMend(params).then(data => {
                this.mendInfo = data.mainTableInfo;
                this.mendForm && !this.mendForm.isFormInit && this.mendForm.initFormFields(data.mainTableInfo);
            })
        }
    }

    //确认送修
    doCptMendSure = (type, callBack) => {
        this.mendForm.validateForm().then( f =>{
            if(f.isValid){
                const fields = toJS(this.mendForm.getFormParams());
                Apis.doMendSure({ capitalid: this.capitalid, ...fields }).then(data => {
                    this.doMend(false);
                    typeof callBack == "function" && callBack();//刷新查询列表
                    this.doSearch();//刷新我的资产列表
                    this.getCptInfo({ viewtype: "view", capitalid: this.capitalid });
                    { type == "card" && window.opener._table.reLoad(); }
                })
            }else{
                f.showErrors();
            }
        });
    }
    //共享设置
    addCptShare = (params = {}) => {
        Apis.addCptShare({ capitalid: this.capitalid, ...params }).then(data => {
            if(data.msg==''){
                message.success(getLabel('385412', "添加共享成功！"));
            }else{
                message.error(data.msg);
            }
        })
    }
    
    //日志
    showCptLogs = (bool, id) => {
        this.showlog = bool;
        if (bool) {
            if (toJS(this.logCondition).length <= 0) {
                this.logForm = new WeaForm();
                Apis.getlogCondition({ capitalid: id }).then(data => {
                    this.logCondition = data.condition;
                    this.logForm && !this.logForm.isFormInit && this.logForm.initFormFields(data.condition);
                })
            }
        } else {
            this.logCondition = [];
        }
    }
    logListSearch = (params = {}) => {
        const searchParamsAd = this.logForm.getFormParams();
        const newParams = { capitalid: this.capitalid, ...searchParamsAd, ...params };
        for (let item in newParams) {
            if (newParams[item] == "") {
                delete newParams[item]
            }
        }
        Apis.getlogList(newParams).then(data => {
            this.logTableStore.getDatas(data.sessionkey, params.current || 1);
        })
    }
    //编辑
    showCptInfo = (bool, params = {}) => {
        this.showEditDialog = bool;
        if (bool) {
            this.getCptInfo(params);
        }
    }
    //获取资产信息
    getCptInfo = (params = {}) => {
        this.cptForm = new WeaForm();
        this.capitalid = params.capitalid;
        Apis.getCptInfo(params).then(data => {
            this.cptInfo = { ...this.cptInfo, ...data };
            this.cptForm && !this.cptForm.isFormInit && this.cptForm.initFormFields(data.fieldinfo);
            this.cptForm.isFormInit && this.cptForm.updateFields(getFormInitDatas(data.fieldinfo), true);
            this.position = data.position;
            this.address = data.address;
        });
        Apis.getCardRightMenu({ ...params, tabkey: "0" }).then(data => {
            this.cptInfo = { ...this.cptInfo, ...data };
        })
    }
    //保存规则
    setValidate = (params = {}) => {
        this.validateRules = params;
    }
    //编辑保存
    saveCptInfo = (params = {}, type = "list") => {
        this.cptForm.validateForm().then(f=>{
            if(f.isValid){
                const searchParamsAd = this.cptForm.getFormParams();
                const newParams = { capitalid: this.capitalid, ...searchParamsAd, ...params };
                if (this.check_form) {
                    Apis.saveCptEdit(newParams).then(data => {
                        if (data.msgid == "1") {
                            Modal.info({
                                title: getLabel(15172, '系统提示'),
                                content: data.msg,
                            });
                        } else {
                            this.showCptInfo(true, { capitalid: this.capitalid, viewtype: 'view' });
                            if (type !== "card") {
                                window._table.reLoad();
                            } else {
                                if (window.opener && window.opener._table && window.opener._table.reLoad) {
                                    window.opener._table.reLoad();
                                }
                            }
                        }
                    })
                }
            }else{
                f.showErrors();
            }
        });
    }
    //编辑保存-jiaoyan
    check_form = () => {
        const fields = toJS(this.cptForm.getFormParams());
        let checkstr = toJS(this.cptInfo.checkstr);
        let checkitem = checkstr.split(",");
        for (i = 0; i < checkitem.length; i++) {
            let checkid = checkitem[i];
            if (checkid) {
                if (!fields[checkid].value) {
                    // Modal.warning({
                    //     title: getLabel(15172, '系统提示'),
                    //     content: getLabel(30933, '必填信息不完整!'),
                    // });
                    message.error(getLabel(30933, '必填信息不完整!'));
                    return false;
                }
            }
        }
        return true;
    }

    showCptFlowModify = (bool, params = {}, type) => {
        this.flowTableStore = new TableStore();
        this.modifyTableStore = new TableStore();

        this.showFlowModify = bool;

        if (bool) {
            if (type == "flow") {
                this.showType = "flow";
                this.getFlowLogList(params);  //流转日志列表
                this.getFlowCondition(params);  //流转日志高级搜索
            } else if (type == "modify") {
                this.showType = "modify";
                this.getModifyList(params);  //变更记录列表
                this.getModifyCondition(params);  //变更记录高级搜索
            }
        }

    }
    getFlowLogList = (params = {}) => {
        this.showType = "flow";
        const searchParamsAd = this.flowForm.getFormParams();
        if (params.capitalid && this.capitalid !== params.capitalid) {
            this.capitalid = params.capitalid;
        }
        const newParams = { capitalid: this.capitalid, ...searchParamsAd, ...params };
        for (let item in newParams) {
            if (newParams[item] == "") {
                delete newParams[item]
            }
        }
        this.loading = true;
        Apis.getCptFlowList(newParams).then(data => {
            if (!data.isright && typeof (data.isright) !== "undefined") {
                this.hasFlowRight = data.isright;
                this.hasVerified = true;
            } else {
                this.hasFlowRight = true;
                this.hasVerified = true;
                this.flowTableStore.getDatas(data.sessionkey, params.current || 1);
            }
            this.loading = false;
        })
    }

    getFlowCondition = (params = {}) => {
        this.flowForm = new WeaForm();
        this.cptCardCondition = [];
        Apis.getCptCardCondition({ adtype: 'flow', ...params }).then(data => {
            this.cptCardCondition = data.condition;
            this.flowForm && !this.flowForm.isFormInit && this.flowForm.initFormFields(data.condition);
        });
    }

    getModifyList = (params = {}) => {
        this.showType = "modify";
        const searchParamsAd = this.modifyForm.getFormParams();
        if (params.capitalid && this.capitalid !== params.capitalid) {
            this.capitalid = params.capitalid;
        }
        const newParams = { capitalid: this.capitalid, ...searchParamsAd, ...params };
        for (let item in newParams) {
            if (newParams[item] == "") {
                delete newParams[item]
            }
        }
        this.loading = true;
        Apis.getCptModifyList(newParams).then(data => {
            this.modifyTableStore.getDatas(data.sessionkey, params.current || 1);
            this.loading = false;
        })
    }

    getModifyCondition = (params = {}) => {
        this.modifyForm = new WeaForm();
        this.cptModifyCondition = [];
        Apis.getCptCardCondition({ adtype: 'modify', ...params }).then(data => {
            this.cptModifyCondition = data.condition;
            this.modifyForm && !this.modifyForm.isFormInit && this.modifyForm.initFormFields(data.condition);
        });
    }
    //查看流转日志详情
    showCptFlowDetail = (bool, id) => {
        this.cptFlowDeltail = bool;
        if (bool) {
            this.flowDetailForm = new WeaForm();
            this.flowDetailCondition = [];
            Apis.getCptFlowDetail({ capitalid: id }).then(data => {
                this.flowDetailCondition = data.fieldinfo;
                this.flowDetailForm && !this.flowDetailForm.isFormInit && this.flowDetailForm.initFormFields(data.fieldinfo);
            })
        }
    }

    @action
	setModifylogVisible = (v)=>{
		this.modifylogVisible = v;
	}

	@action
	getModifyLog = (capitalid)=>{
		Apis.getCptModifyList({capitalid}).then(data => {
			this.modifyTableStore.getDatas(data.sessionkey, 1);
			this.modifylogVisible = true;
			this.getModifyCondition({capitalid:this.capitalid});  //变更记录高级搜索
		})
    }
    
    clearFormFields1 = () => {
        if (this.showType == "flow") {
            this.flowForm.reset();
        } else {
            this.modifyForm.reset();
        }
    }

    onShowColumn = () => {
        this.tableStore.setColSetVisible(true);
        this.tableStore.tableColSet(true)
    }

    clearStatus = () => {
        this.searchParams = { name: "" };
        this.showSearchAd = false;
        this.selectedTreeKey = "";
        this.clearFormFields();
        this.baseParams = {};
        this.rightMenu = [];
        this.condition = [];
        this.conditiontype = { type: "mycpt" };
        this.capitalid = "";
        this.treeSearchParams = {};
    }

}

export default MineCptStore;